package com.example.seabedmusic.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.entity.NotificationSettingItem;
import java.util.List;

public class NotificationSettingsAdapter extends RecyclerView.Adapter<NotificationSettingsAdapter.VH> {

    private final List<NotificationSettingItem> list;
    private OnSwitchChangeListener listener;
    private boolean itemsEnabled = true;

    public interface OnSwitchChangeListener {
        void onSwitchChanged(int position, boolean isChecked);
    }

    public NotificationSettingsAdapter(List<NotificationSettingItem> list) {
        this.list = list;
    }

    public void setOnSwitchChangeListener(OnSwitchChangeListener listener) {
        this.listener = listener;
    }

    public void setItemsEnabled(boolean enabled) {
        this.itemsEnabled = enabled;
        notifyDataSetChanged();
    }

    public void setItemChecked(int position, boolean checked) {
        if (position >= 0 && position < list.size()) {
            list.get(position).setEnabled(checked);
            notifyItemChanged(position);
        }
    }

    public NotificationSettingItem getItemAtPosition(int position) {
        if (position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView tvTitle, tvDescription;
        Switch switchSetting;
        View divider;

        VH(View v) {
            super(v);
            tvTitle = v.findViewById(R.id.tvTitle);
            tvDescription = v.findViewById(R.id.tvDescription);
            switchSetting = v.findViewById(R.id.switchSetting);
            divider = v.findViewById(R.id.divider);
        }
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_setting_switch, parent, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        NotificationSettingItem item = list.get(position);

        holder.tvTitle.setText(item.getTitle());
        holder.tvDescription.setText(item.getDescription());
        holder.switchSetting.setChecked(item.isEnabled());

        // 根据总开关设置其他项的可用性
        if (position > 0) {
            holder.tvTitle.setEnabled(itemsEnabled);
            holder.tvDescription.setEnabled(itemsEnabled);
            holder.switchSetting.setEnabled(itemsEnabled);
        }

        // 隐藏最后一项的分割线
        if (position == list.size() - 1) {
            holder.divider.setVisibility(View.GONE);
        } else {
            holder.divider.setVisibility(View.VISIBLE);
        }

        holder.switchSetting.setOnCheckedChangeListener(null); // 防止重复监听
        holder.switchSetting.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (listener != null) {
                    listener.onSwitchChanged(holder.getAdapterPosition(), isChecked);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}